package com.xin.web;

import com.xin.pojo.Cart;
import com.xin.pojo.User;
import com.xin.service.OrderService;
import com.xin.service.impl.OrderServiceimpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class OrderServlet extends BaseServlet{
    private OrderService orderService=new OrderServiceimpl();
    protected void createOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        User user = (User) req.getSession().getAttribute("user");
        if (user==null){
            //说明用户还没有登录，请求转发到登录页面
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req,resp);
            return;
        }
        Integer id = user.getId();
        req.getSession().setAttribute("orderId",orderService.createOrder(cart,id));
        resp.sendRedirect(req.getContextPath()+"/pages/cart/checkout.jsp");
    }
}
